<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 快递
 *
 * @author      Eden
 * @datetime    2016/12/10 18:06
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Express_Model extends ADM_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 设置快递状态
     * @param array $params
     * @return bool
     */
    public function setExpress($params = array())
    {
        try {
            $orderId = $params['orderId'];
            if (empty($orderId)) {
                return false;
            }
            unset($params['orderId']);
            $params['created'] = dateTime();

            // 修改表
            $eDb = clone($this->db);
            $eDb->trans_start();
            $query = $eDb->get_where('user_order_express', array('orderId' => $orderId));
            if ($query->row_array()) {
                $eDb->where(array('orderId' => $orderId));
                $eDb->update('user_order_express', $params);
            } else {
                $eDb->insert('user_order_express', array_merge($params, array('orderId' => $orderId)));
            }

            // 修改状态
            $mDb = clone($this->db);
            $mDb->where(array('id' => $orderId));
            $mDb->update('user_order', array('status' => 'Dispatch'));
            $eDb->trans_complete();

            if ($eDb->trans_status() !== false) {
                return true;
            }
        } catch(PDOException $e) {}
        return false;
    }

}
